<?php


namespace catchAdmin\tenant\controller;


use catchAdmin\tenant\model\MyPorts;
use catchAdmin\tenant\request\CreatePortsRequest;
use catcher\base\CatchController;
use catcher\base\CatchRequest as Request;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\Utils;

class MyPort extends CatchController
{
    protected $my_ports;

    public function __construct(myPorts $my_ports)
    {
        $this->my_ports = $my_ports;
    }

    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function portList(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->my_ports->getList($user['tenant_id']));
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function createPort(CatchAuth $auth,CreatePortsRequest $request)
    {
        $params = $request->param();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        $flag = $this->my_ports->where(['port_id'=>$params['port_id'],'tenant_id'=>$params['tenant_id']])->find();
        if($flag){
            return CatchResponse::fail('口岸已存在');
        }else{
            $this->my_ports->storeBy($params);
            return CatchResponse::success('', '新建成功');
        }

    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function updatePort(Request $request)
    {
        $params = $request->filterEmptyField()->param();
        $id = $params['id'];
        $this->my_ports->updateBy($id, $params);
        return CatchResponse::success('', '编辑成功');
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function delPort(Request $request)
    {
        $id = $request->post('id');
        $ids = Utils::stringToArrayBy($id);
        foreach ($ids as $_id) {
            $this->my_ports->deleteBy($_id);
        }
        return CatchResponse::success('', '删除成功');
    }
}